package com.ts.oss.controller;

import com.ts.oss.service.OssFileService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "文件上传")
@RestController
@RequestMapping("/oss/files")
public class OssController {
    @Resource
    private OssFileService ossService;





    //上传头像的方法
    @PostMapping("/upload")
    public Map<String,Object> uploadOssFile(@RequestParam("file") MultipartFile file)
    {
        //获取上传文件
        //返回上传到oss路径
        String s = ossService.uploadFileAvatar(file);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("messsage","成功");
        



        HashMap<String, Object> data = new HashMap<>();
        data.put("url",s);
        map.put("data",data);
        return map;

    }

    //处理字节数组的API
    @PostMapping("/uploadByte")
    public Map<String,Object> uploadByteImage(@RequestBody byte[] data){
        String url=ossService.uploadByteFile(data);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",url);
        map.put("messsage","成功");
        return map;

    }


}
